package com.baosight.sczx.zm.cc.service; 
import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemHandle;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemHandleProcess_x; 
/**
 * 生产问题处理---过程记录
 */
public class ServiceZMCC13 extends ServiceEPBase {
	/**
	 * 初始化 
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo); 
		return outInfo;
	}
	/**
	 * 查询
	 */
	public EiInfo query(EiInfo inInfo)
	{
		
		String keyId = inInfo.getString("fdKeyId");
		EiInfo outInfo=inInfo;
		if(keyId!=null)
		{
			StaticTools.setQueryParameters(inInfo, "fdRecordId", keyId, true);//添加查询条件
//			outInfo = super.query(inInfo, "ZMCCProduceproblemNeedModify_x.query", new ZMCCProduceproblemNeedModify_x());
			outInfo = super.query(inInfo, "ZMCCProduceproblemHandleProcess_x.query", new ZMCCProduceproblemHandleProcess_x());
//			String aa = outInfo.getBlock("result").getRow(0).get("fdCreateTimeM").toString();
//			String bb = outInfo.getBlock("result").getRow(0).get("fdCreateTimeR").toString();
//			System.out.println("整改创建时间："+aa);
//			System.out.println("复查创建时间："+bb);
		}else
		{
			outInfo.addBlock("result").setBlockMeta(new ZMCCProduceproblemHandleProcess_x().eiMetadata);
		}

//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));//部门
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));//部门
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType"));
		return outInfo;
	}
	
	
	/**
	 * 新增方法 
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCCProduceproblemHandle _dom = new ZMCCProduceproblemHandle();
		EiBlock eiBlock = inInfo.getBlock("solve_add");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM_HANDLE"));
			this.dao.insert("ZMCCProduceproblemHandle.insert", _dom);

			// 改主表状态 未查收
			Map<String, String> params1 = new HashMap<String, String>();
			params1.put("fdKeyId", _dom.getFdRecordId());
			this.dao.update("ZMCCProduceproblem.modifyByNotReceive", params1);

			// 更新主表 是否罚款字段
			Map<String, String> params2 = new HashMap<String, String>();
			params2.put("fdKeyId", _dom.getFdRecordId());
			params2.put("fdIsPenalty", _dom.getFdIsPenalty());
			this.dao.update("ZMCCProduceproblem.updatePenalty", params2);

			this.dao.update("ZMCCProduceproblemHandle.updatePenalty", params1);

			inInfo.set("fdKeyId", _dom.getFdRecordId());
			inInfo.set("fdRecordId", _dom.getFdRecordId());
			inInfo.set("fdHandleId", _dom.getFdKeyId());

		}
		out = this.query(inInfo); 
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	} 
}
